/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jaiz.twitter.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jaiz.twitter.dao.TweetDAO;
import org.jaiz.twitter.dao.UsuarioDAO;
import org.jaiz.twitter.model.Tweet;
import org.jaiz.twitter.model.Usuario;
import org.jaiz.twitter.util.Query;

/**
 *
 * @author JesusMata
 */
public class TweetService {

    private UsuarioDAO udao;
    private TweetDAO tdao;
    private static int tweetsListed = 20;
    
    public enum users{FOLLOW, FOLLOWERS};

    public TweetService() {
        udao = new UsuarioDAO();
        tdao = new TweetDAO();
    }

    public void addTweet(Tweet tweet) {
        formatTweet(tweet);
        tdao.create(tweet);
    }
    
    private void formatTweet(Tweet t){
        String text = t.getTexto();
        StringBuffer stringBuffer = new StringBuffer();
        Pattern p = Pattern.compile("\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
        Matcher m = p.matcher(text);
        while (m.find()) { //Por todos los que cumplan con el regex
            m.appendReplacement(stringBuffer, "<a href='"+m.group()+"'>" + m.group() + "</a>");
        }
        m.appendTail(stringBuffer);
        System.out.println(stringBuffer);
        t.setTexto(stringBuffer.toString());
    }
    
    /*
     * Method for retrive the newest tweets
     */
    public Collection<Tweet> getRecentTweets() {
        List<Tweet> tweets = new ArrayList<Tweet>();
        try {
            PreparedStatement statement = udao.createStatement(Query.getQuery("tweet.getRecent"));
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                Tweet tweet = new Tweet();
                tweet.setId(rs.getInt("idTweet"));
                tweet.setUsuario(udao.getById(rs.getString("usuario")));
                tweet.setTexto(rs.getString("texto"));
                tweet.setFecha(rs.getDate("fecha"));
                tweets.add(tweet);
            }
            return tweets;
        } catch (SQLException ex) {
            Logger.getLogger(TweetService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public Collection<Tweet> getUserTweets(Usuario usuario) {
        List<Tweet> tweets = new ArrayList<Tweet>();
        try {
            PreparedStatement statement = udao.createStatement(Query.getQuery("tweet.getByUser"));
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                Tweet tweet = new Tweet();
                tweet.setId(rs.getInt("idTweet"));
                tweet.setUsuario(udao.getById(rs.getString("usuario")));
                tweet.setTexto(rs.getString("texto"));
                tweet.setFecha(rs.getDate("fecha"));
                tweets.add(tweet);
            }
            return tweets;
        } catch (SQLException ex) {
            Logger.getLogger(TweetService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }    
    
    private Collection<Usuario> getCollection(){
        return null;
    }
}
